5  Chapter 3 Exercise Solutions

5.1 Solution to Exercise 3.1

Label 365 cards with the numbers 1 through 365. Shuffle the cards and deal \(n\) with replacement. If the \(n\) cards dealt all have different numbers, then \(B\) does not occur; otherwise \(B\) occurs.

5.2 Solution to Exercise 3.2

Create a spinner with two sectors, one sector labeled “make” which accounts for 40% of the area, and the other 60% labeled “miss”. Spinner the spinner until it lands on “make” and then stop; let \(X\) be the total number of spins.

5.3 Solution to Exercise 3.3

Create a spinner with three equal sectors, labeled 1, 2, 3. Spin the spinner 3 times. Let \(X\) be the number of distinct numbers the spinner lands on; for example if the spinner lands on 3 then 1 then 3, then \(X\) is 2. Let \(Y\) be the number of spins that land on 1.

5.4 Solution to Exercise 3.4

  1. A Uniform(0, 12) spinner is basically a clock. It goes from 0 to 12 in equally spaced increments. See Figure 5.1.
  2. To simulate \(X\), flip the coin to determine if the point is in the “east” or “west” and then spin the Uniform(0, 12) spinner to determine how far from the center in the east or west direction. Let the result of the spin be \(U_1\). If the coin lands on heads (east) set \(X=U_1\); if the coin lands on tails (west) set \(X=-U_1\). To simulate \(Y\), spin the spinner again to get \(U_2\), and flip the coin again. If the result of the flip is heads (north) then set \(Y = U_2\); if the result of the flip is tails (south) then set \(Y=-U_2\). Compute the distance from \((X, Y)\) to (0, 0) as \(R=\sqrt{X^2+Y^2}\). If \(R>12\) the dart would land off the board, so just discard the repetition and try again.
  3. Exercise 2.13 shows that \(R\) is not uniformly distributed between 0 and 12. In particular, \(R\) is more likely to be between 11 and 12 than it is to be between 0 and 1. Therefore a Uniform(0, 12) spinner would not be appropriate.
Figure 5.1: A continuous Uniform(0, 12) spinner. Only selected rounded values are displayed, but in the idealized model the spinner is infinitely precise so that any real number between 0 and 12 is a possible outcome.

5.5 Solution to Exercise 3.5

Figure 5.2: Spinner representing the distribution of \(R\) in Exercise 3.5

5.6 Solution to Exercise 3.6

P = BoxModel([1, 2, 3], size = 3)

def count_distinct(u):
    return len(set(u))

X = RV(P, count_distinct)

Y = RV(P, count_eq(1))

(RV(P) & X & Y).sim(10)
Index Result
0((1, 1, 3), 2, 2)
1((3, 1, 1), 2, 2)
2((2, 1, 2), 2, 1)
3((3, 1, 2), 3, 1)
4((2, 1, 3), 3, 1)
5((1, 3, 2), 3, 1)
6((1, 2, 2), 2, 1)
7((3, 3, 3), 1, 0)
8((1, 2, 3), 3, 1)
......
9((1, 2, 3), 3, 1)

5.7 Solution to Exercise 3.7

P = BoxModel([1, 2, 3], size = 3, probs = [0.1, 0.3, 0.6])

def count_distinct(u):
    return len(set(u))

X = RV(P, count_distinct)

Y = RV(P, count_eq(1))

(RV(P) & X & Y).sim(10)
Index Result
0((2, 3, 2), 2, 0)
1((2, 3, 2), 2, 0)
2((2, 2, 2), 1, 0)
3((3, 2, 2), 2, 0)
4((2, 1, 2), 2, 1)
5((3, 3, 2), 2, 0)
6((2, 1, 2), 2, 1)
7((3, 3, 2), 2, 0)
8((3, 3, 3), 1, 0)
......
9((3, 2, 3), 2, 0)

5.8 Solution to Exercise 3.8

The code below defines a random variable \(X\) that counts the number of distinct birthdays in the group on \(n\). Event \(B\) occurs if \(X<n\).

n = 30

P = BoxModel(list(range(365)), size = n)

def count_distinct(u):
  return len(set(u))

X = RV(P, count_distinct)

B = (X < n)

B.sim(10)
Index Result
0True
1True
2False
3False
4True
5True
6True
7True
8False
......
9False

5.9 Solution to Exercise 3.9

p_success = 0.4

# define a function that takes as an input a sequence of 0s and 1s (omega)
# and returns when the first 1 occurs
def count_until_first_success(omega):
    for i, w in enumerate(omega):
        if w == 1:
            return i + 1 # the +1 is for zero-based indexing

# Either of the following probability spaces works        
# P = Bernoulli(p_success) ** inf
P = BoxModel([1, 0], probs = [p_success, 1 - p_success], size = inf)

X = RV(P, count_until_first_success)

(RV(P) & X).sim(10)
Index Result
0((0, 0, 0, 0, 0, 1, ...), 6)
1((0, 1, 0, 0, 1, 1, ...), 2)
2((0, 0, 1, 1, 0, 1, ...), 3)
3((0, 0, 0, 0, 1, 1, ...), 5)
4((0, 0, 0, 0, 1, 0, ...), 5)
5((1, 0, 1, 0, 1, 0, ...), 1)
6((0, 0, 0, 0, 0, 1, ...), 6)
7((0, 1, 0, 0, 0, 0, ...), 2)
8((1, 1, 1, 0, 0, 0, ...), 1)
......
9((0, 1, 0, 0, 0, 1, ...), 2)

5.10 Solution to Exercise 3.10

In the code below

  • Uniform(0, 12) ** 2 corresponds to the two spins
  • BoxModel([-1, 1], size = 2) corresponds to the two coin flips
  • Uniform(0, 12) ** 2 * BoxModel([-1, 1], size = 2) simulates a pair which we unpack and define as RVs U, F, but U is itself a pair and so is F.
  • U[0] is the first spin and F[0] is the first flip.
U, F = RV(Uniform(0, 12) ** 2 * BoxModel([-1, 1], size = 2))

X = U[0] * F[0]

Y = U[1] * F[1]

R = sqrt(X ** 2 + Y ** 2)

(U & F & X & Y & R).sim(10)
Index Result
0((7.987604405609741, 7.157091856000399), (-1, -1), -7.987604405609741, -7.157091856000399, 10.725007...
1((7.652625149976469, 0.8147167016012169), (-1, -1), -7.652625149976469, -0.8147167016012169, 7.69587...
2((6.485075466044826, 1.330659022251786), (-1, -1), -6.485075466044826, -1.330659022251786, 6.6201855...
3((6.194222129963676, 10.1536345121395), (1, -1), 6.194222129963676, -10.1536345121395, 11.8938926177...
4((6.3844746334913065, 3.8260811172388896), (1, 1), 6.3844746334913065, 3.8260811172388896, 7.4431453...
5((2.632344228387563, 8.159476762990888), (-1, 1), -2.632344228387563, 8.159476762990888, 8.573581351...
6((9.1645244754617, 0.1515800434824035), (-1, 1), -9.1645244754617, 0.1515800434824035, 9.16577794684...
7((10.774643800797215, 11.745275274156855), (-1, 1), -10.774643800797215, 11.745275274156855, 15.9387...
8((1.0740168505595227, 8.501284951120132), (-1, -1), -1.0740168505595227, -8.501284951120132, 8.56885...
......
9((8.699942936763772, 3.3778807849144243), (-1, -1), -8.699942936763772, -3.3778807849144243, 9.33268...

Currently the \((X, Y)\) pairs are uniformly distributed in the box with sides [-12, 12]. We’ll see how to discard points off the board later.

plt.figure()
(X & Y).sim(1000).plot()
plt.show()

5.11 Solution to Exercise 3.11

P = Uniform(1, 4) ** 2

X = RV(P, sum)
Y = RV(P, max)

(RV(P) & X & Y).sim(10)
Index Result
0((3.89773058044035, 3.2268349649180093), 7.12456554535836, 3.89773058044035)
1((3.99519028703384, 1.235475951288426), 5.230666238322266, 3.99519028703384)
2((2.0562301958582228, 3.520035417417259), 5.576265613275481, 3.520035417417259)
3((1.9425366409915221, 2.433110909353821), 4.375647550345343, 2.433110909353821)
4((2.579482083141915, 2.2017923953887903), 4.7812744785307055, 2.579482083141915)
5((1.152590784567175, 2.5785760368096504), 3.731166821376825, 2.5785760368096504)
6((2.1141347332946836, 3.9064908886409153), 6.020625621935599, 3.9064908886409153)
7((2.704768126203829, 1.9756685976817914), 4.68043672388562, 2.704768126203829)
8((3.221155879367371, 2.730791871667036), 5.951947751034407, 3.221155879367371)
......
9((1.3342229715730243, 1.3802142784819276), 2.714437250054952, 1.3802142784819276)
plt.figure()
(X & Y).sim(1000).plot()
plt.show()

5.12 Solution to Exercise 3.12

Part 1

See previous solutions which discuss how to simulate \((X, Y)\) pairs.

Simulate many \((X, Y)\) pairs, say 10000.

  1. To approximate \(\textrm{P}(X = 2)\): Divide the number of repetitions with \(X = 2\) by the total number of repetitions
  2. To approximate \(\textrm{P}(Y = 1)\): Divide the number of repetitions with \(Y = 1\) by the total number of repetitions
  3. To approximate \(\textrm{P}(X = 2, Y = 1)\): Divide the number of repetitions with \(X = 2\) and \(Y=1\) by the total number of repetitions

Part 2

P = BoxModel([1, 2, 3], size = 3)

def count_distinct(u):
    return len(set(u))

X = RV(P, count_distinct)

Y = RV(P, count_eq(1))

x_and_y = (X & Y).sim(10000)

x = x_and_y[0]

y = x_and_y[1]
x.count_eq(2) / x.count()
0.6619
y.count_eq(1) / x.count()
0.4446
((x == 2) * (y == 1)).sum() / x.count()
0.2189

Part 3

x.tabulate(normalize = True)
Value Relative Frequency
10.1124
20.6619
30.2257
Total1.0
plt.figure()
x.plot()
plt.show()

y.tabulate(normalize = True)
Value Relative Frequency
00.2904
10.4446
20.2274
30.0376
Total1.0
plt.figure()
y.plot()
plt.show()

x_and_y.tabulate(normalize = True)
Value Relative Frequency
(1, 0)0.0748
(1, 3)0.0376
(2, 0)0.2156
(2, 1)0.2189
(2, 2)0.2274
(3, 1)0.2257
Total1.0
plt.figure()
x_and_y.plot('tile')
plt.show()

5.13 Solution to Exercise 3.13

Part 1

See previous solutions which discuss how to simulate \((X, Y)\) pairs.

Simulate many \((X, Y)\) pairs, say 10000.

  1. To approximate \(\textrm{P}(X = 2)\): Divide the number of repetitions with \(X = 2\) by the total number of repetitions
  2. To approximate \(\textrm{P}(Y = 1)\): Divide the number of repetitions with \(Y = 1\) by the total number of repetitions
  3. To approximate \(\textrm{P}(X = 2, Y = 1)\): Divide the number of repetitions with \(X = 2\) and \(Y=1\) by the total number of repetitions

Part 2

P = BoxModel([1, 2, 3], size = 3, probs = [0.1, 0.3, 0.6])

def count_distinct(u):
    return len(set(u))

X = RV(P, count_distinct)

Y = RV(P, count_eq(1))

x_and_y = (X & Y).sim(10000)

x = x_and_y[0]

y = x_and_y[1]
x.count_eq(2) / x.count()
0.6424
y.count_eq(1) / x.count()
0.242
((x == 2) * (y == 1)).sum() / x.count()
0.1329

Part 3

x.tabulate(normalize = True)
Value Relative Frequency
10.2485
20.6424
30.1091
Total1.0
plt.figure()
x.plot()
plt.show()

y.tabulate(normalize = True)
Value Relative Frequency
00.7286
10.242
20.0276
30.0018
Total1.0
plt.figure()
y.plot()
plt.show()

x_and_y.tabulate(normalize = True)
Value Relative Frequency
(1, 0)0.2467
(1, 3)0.0018
(2, 0)0.4819
(2, 1)0.1329
(2, 2)0.0276
(3, 1)0.1091
Total1.0
plt.figure()
x_and_y.plot('tile')
plt.show()

5.14 Solution to (approximate-probability-geometric?)

Part 1

See previous solutions for description of how to simulate values of \(X\).

Simulate many values of \(X\) and summarize the simulate values and their relative frequencies to approximate the distribution of \(X\).

To approximate \(\textrm{P}(X > 3)\): divide the number of repetitions with \(X>3\) by the total number of repetitions.

Part 2

p_success = 0.4

# define a function that takes as an input a sequence of 0s and 1s (omega)
# and returns when the first 1 occurs
def count_until_first_success(omega):
    for i, w in enumerate(omega):
        if w == 1:
            return i + 1 # the +1 is for zero-based indexing

# Either of the following probability spaces works        
# P = Bernoulli(p_success) ** inf
P = BoxModel([1, 0], probs = [p_success, 1 - p_success], size = inf)

X = RV(P, count_until_first_success)

x = X.sim(10000)

x
Index Result
01
16
28
31
41
51
61
71
81
......
99992
x.tabulate(normalize = True)
Value Relative Frequency
10.3992
20.2416
30.1435
40.087
50.0508
60.0296
70.0194
80.0112
90.0065
100.0048
110.0027
120.0015
130.0011
140.0005
150.0004
200.0001
220.0001
Total1.0
plt.figure()
x.plot()
plt.show()

x.count_gt(3) / x.count()
0.2157

5.15 Solution to Exercise 3.15

Part 1

See previous solutions for how to simulate \((X, Y)\) pairs.

Simulate many \((X, Y)\) pairs, say 10000. To approximate:

  1. \(\textrm{P}(X < 3.5)\): divide number of repetitions with \(X<3.5\) by the total number of repetitions
  2. \(\textrm{P}(Y > 2.7)\): divide number of repetitions with \(Y>2.7\) by the total number of repetitions
  3. \(\textrm{P}(X < 3.5, Y > 2.7)\): divide number of repetitions with \(X<3.5\) and \(Y>2.7\) by the total number of repetitions

Part 2

We usually summarize simulated values of continuous random variables with histogram.

P = Uniform(1, 4) ** 2

X = RV(P, sum)
Y = RV(P, max)

x_and_y = (X & Y).sim(10000)

x = x_and_y[0]

y = x_and_y[1]
plt.figure()
x.plot()
plt.show()

plt.figure()
y.plot()
plt.show()

plt.figure()
x_and_y.plot()
plt.show()

plt.figure()
x_and_y.plot('hist')
plt.show()